/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: address.hpp
 */

#ifndef _ADDRESS_HPP_
#define _ADDRESS_HPP_

#include <iostream>
#include <string>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
typedef std::string InetIpAddr;
typedef unsigned int InetPort;
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*!
 * Represents an INET address.
 */
class Address {
public:
	/*!
	 * Constructs a private loopback address to an invalid port.
	 */
	Address() {
		this->_ip = "127.0.0.1";
		this->_port = 0;
	}
	/*!
	 * Constructs an address basing on an ip and port
	 */
	Address(const InetIpAddr& ip, const InetPort port) {
		this->_ip = ip;
		this->_port = port;
	}
	/*!
	 * Destructor
	 */
	~Address() {

	}
	/*!
	 * Gets ip address.
	 */
	InetIpAddr get_ip() const {
		return this->_ip;
	}
	/*!
	 * Sets ip address.
	 */
	void set_ip(InetIpAddr ip) {
		this->_ip = ip;
	}
	/*!
	 * Gets port.
	 */
	InetPort get_port() const {
		return this->_port;
	}
	/*!
	 * Sets port.
	 */
	void set_port(InetPort port) {
		this->_port = port;
	}
	/*!
	 * Comparison between two addresses.
	 */
	bool operator==(const Address& addr) const {
		bool res = true;
		res = this->_ip == addr._ip;
		res = res && (this->_port == this->_port);
		return res;
	}
	bool operator!=(const Address& addr) const {
		return (*this == addr);
	}
private:
	friend class boost::serialization::access;
	/*!
	 * BOOST serialization function.
	 */
	template<class Archive>
	void serialize(Archive & ar, const unsigned int version) {
		ar & _ip;
		ar & _port;
	}
	/*!
	 * Holds ip address in the form x1x2x3.y1y2y3.z1z2z3.k1k2k3
	 */
	InetIpAddr _ip;
	/*!
	 * Holds port.
	 */
	InetPort _port;
}; /* Address */
} /* middleware */

#endif
